package app

import (
	"oc_saas/db/mysql"
	"oc_saas/glo"
	"oc_saas/model/admin"
	"oc_saas/model/share"
	"os"

	"gorm.io/gorm"
)

type ocOrm struct{}

func (*ocOrm) initOrm() *gorm.DB {
	switch glo.OC_CONF.Server.DBType {
	// 观察现在是使用哪种类型的数据库
	case "mysql":
		return mysql.OrmMysql()
	default:
		return nil
	}
}

func (*ocOrm) migrate(db *gorm.DB) {
	if err := db.AutoMigrate(
		&share.Rule{},
		&share.Route{},
		&share.Api{},
		&share.Params{},
		&share.Role{},
		&share.RoleRule{},
		&admin.User{},
		&admin.Application{},
		&admin.TenancyDB{},
	); err != nil {
		os.Exit(0)
	}
}

func (*ocOrm) SetAllDbs() {
	glo.OC_ORM_CLUSTER = mysql.AllDbConnect(glo.OC_DB)
}

var ocOrmApp = new(ocOrm)
